package com.xingkeduo.controller.cms.activity;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xingkeduo.controller.base.ActivityBaseController;
import com.xingkeduo.model.param.ActivityParam;
import com.xingkeduo.model.param.ActivityParam.ActivityModeEnum;
import com.xingkeduo.model.param.ActivityParam.ActivityTypeEnum;
import com.xingkeduo.model.param.MarketingParam.MarketingCouponStatusEnum;
import com.xingkeduo.model.param.MarketingParam.MarketingReceiveTypeEnum;
import com.xingkeduo.model.param.MarketingParam.MarketingStatusEnum;
import com.xingkeduo.service.cms.activity.ActivityMarketingTemplateService;
import com.xingkeduo.util.Constant;
import com.xingkeduo.util.tool.ResponseMessage;

/**
 * 
* @ClassName: HairdresserTemplateCmsController
* @Description: 发型师活动相关-后台接口
* @author lixin
* @date May 3, 2019 3:13:46 PM
*
 */
@RestController
@RequestMapping(Constant.CMS_URL_PREFIX + "/activity/hairdresser")
public class HairdresserTemplateCmsController extends ActivityBaseController {

	@Autowired
	ActivityMarketingTemplateService activityMarketingTemplateService;

	@RequestMapping("/addHairdresserActivityTemplate")
	public ResponseMessage addHairdresserActivityTemplate(@RequestBody ActivityParam hairdresserActivityTemplate) {
		hairdresserActivityTemplate.setActivityMode(ActivityModeEnum.HAIRDRESSER.getCode());
		hairdresserActivityTemplate.setStatus(MarketingStatusEnum.DOWN.getCode());
		try {
			this.initParams(hairdresserActivityTemplate);
			return activityMarketingTemplateService.saveOrUpdateActivityByTemplate(hairdresserActivityTemplate);
		} catch (Exception e) {
			return ResponseMessage.error(e.getMessage(), 500);
		}
	}

	@RequestMapping("/getHairdresserActivityTemplateDetail")
	public ResponseMessage getHairdresserActivityTemplateDetail(Long hairdresserActivityTemplateId) {
		return activityMarketingTemplateService.queryActivityById(hairdresserActivityTemplateId);
	}

	@RequestMapping("/updateHairdresserActivityTemplate")
	public ResponseMessage updateHairdresserActivityTemplate(@RequestBody ActivityParam hairdresserActivityTemplate) {
		hairdresserActivityTemplate.setActivityMode(ActivityModeEnum.HAIRDRESSER.getCode());
		try {
			this.initParams(hairdresserActivityTemplate);
			return activityMarketingTemplateService.saveOrUpdateActivityByTemplate(hairdresserActivityTemplate);
		} catch (Exception e) {
			return ResponseMessage.error(e.getMessage(), 500);
		}
	}

	@RequestMapping("/auditHairdresserActivityTemplate")
	public ResponseMessage auditHairdresserActivityTemplate(Long hairdresserActivityTemplateId, String status) {
		if (hairdresserActivityTemplateId == null) {
			return ResponseMessage.error("活动id不合法", 500);
		}
		if (MarketingStatusEnum.parseStatus(Integer.parseInt(status)).equals(MarketingStatusEnum.UNKNOWN)) {
			return ResponseMessage.error("活动状态不合法", 500);
		}
		ActivityParam hairdresserActivityTemplate = new ActivityParam();
		hairdresserActivityTemplate.setActivityMode(ActivityModeEnum.HAIRDRESSER.getCode());
		hairdresserActivityTemplate.setId(hairdresserActivityTemplateId);
		hairdresserActivityTemplate.setStatus(Integer.parseInt(status));
		this.initUserInParams(hairdresserActivityTemplate);
		try {
			return activityMarketingTemplateService.updateActivityStatusById(hairdresserActivityTemplate);
		}catch(Exception e) {
			return ResponseMessage.error(e.getMessage(),500);
		}
	}

	@RequestMapping("/getHairdresserActivityemplateList")
	public ResponseMessage getHairdresserActivityemplateList(ActivityParam hairdresserActivityTemplate) {
		hairdresserActivityTemplate.setActivityMode(ActivityModeEnum.HAIRDRESSER.getCode());
		// 活动类型
		if (StringUtils.isNotBlank(hairdresserActivityTemplate.getActivityType())
				&& ActivityTypeEnum.parse(hairdresserActivityTemplate.getActivityType()).equals(ActivityTypeEnum.UNKNOWN)) {
			return ResponseMessage.error("活动类型不合法", 500);
		}
		// 领取范围
		if (hairdresserActivityTemplate.getReceiveType() != null && MarketingReceiveTypeEnum
				.parseStatus(hairdresserActivityTemplate.getReceiveType()).equals(MarketingReceiveTypeEnum.UNKNOWN)) {
			return ResponseMessage.error("活动领取范围不合法", 500);
		}
		// 活动状态
		if (StringUtils.isNotBlank(hairdresserActivityTemplate.getActivityStatus()) && MarketingCouponStatusEnum
				.parse(hairdresserActivityTemplate.getActivityStatus()).equals(MarketingCouponStatusEnum.UNKNOWN)) {
			return ResponseMessage.error("活动上下架状态不合法", 500);
		}
		// 上下架
		if (hairdresserActivityTemplate.getStatus() != null && MarketingStatusEnum
				.parseStatus(hairdresserActivityTemplate.getStatus()).equals(MarketingStatusEnum.UNKNOWN)) {
			return ResponseMessage.error("活动上下架状态不合法", 500);
		}

		return activityMarketingTemplateService.queryActivityByTemplate(hairdresserActivityTemplate);
	}
}
